package be.xios.projectquiz.engine;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import be.xios.projectquiz.statuspanels.*;

/// <summary>
/// Deze klasse is een JFRAME waarop al de verschillende serverpanels worden gedisplayed
/// Deze panels bevatten al de server INFO. Serverstats, connection details, playerstatus etc.
/// </summary>
/// <author>Sebastian Kubski</author>
///	<author>Robin Crombe: Gradient als achtergrond toegevoegd</author>
@SuppressWarnings("serial")
public class GUI extends JFrame {

	private JPanel hoofdpaneel, hoofdpaneel1_1, hoofdpaneel1_2, serverpaneel, jurymasterpaneel, spelerspaneel, spelpaneel;
	private UpdatePanels updatepanels;
	private String[] labelContents = {"Server Status","Connectiondetails","Jury- and Masterstatus","Playersstatus","Gamestatus","Serverlog"};
	private TitledBorder serverStatusBorder, connectionBorder, juryBorder, playerBorder, gameStatusBorder, serverLogBorder;
	private Color matrixKleur = new Color(201,251,164);

	public GUI(String myIP) {
		super("GameEngine");
		
		createBorders();
				
		hoofdpaneel = new JPanel(new GridLayout(2,1)){
            protected void paintComponent(Graphics grphcs) {
                Graphics2D g2d = (Graphics2D) grphcs;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);

                GradientPaint gp = new GradientPaint(0, 0, getBackground().brighter().brighter(), getWidth(), getHeight(), getBackground().darker().darker());

                g2d.setPaint(gp);
                g2d.fillRect(0, 0, getWidth(), getHeight());

				super.paintComponent(grphcs);
            }
        };
        hoofdpaneel.setOpaque(false);
		hoofdpaneel.setBorder(serverStatusBorder);
		
			hoofdpaneel1_1 = new JPanel(new GridLayout(1,4));
			hoofdpaneel1_1.setOpaque(false);
			
				serverpaneel = new JPanel(new BorderLayout());
				serverpaneel.setBorder(connectionBorder);
				serverpaneel.setOpaque(false);
						
					ServerStatusPanel serverstatuspanel = new ServerStatusPanel(myIP, matrixKleur);
					
					serverpaneel.add(serverstatuspanel, BorderLayout.CENTER);
				
				jurymasterpaneel = new JPanel(new BorderLayout());
				jurymasterpaneel.setOpaque(false);
				jurymasterpaneel.setBorder(juryBorder);
								
					JuryMasterStatusPanel jmstatuspanel = new JuryMasterStatusPanel(matrixKleur);
					
					jurymasterpaneel.add(jmstatuspanel, BorderLayout.CENTER);
							
				spelerspaneel = new JPanel(new BorderLayout());
				spelerspaneel.setOpaque(false);
				spelerspaneel.setBorder(playerBorder);
					
					PlayerStatusPanel playerstatuspanel = new PlayerStatusPanel(matrixKleur);
				
					spelerspaneel.add(playerstatuspanel, BorderLayout.CENTER);
						
				spelpaneel =  new JPanel(new BorderLayout());
				spelpaneel.setOpaque(false);
				spelpaneel.setBorder(gameStatusBorder);
					
					GameStatusPanel gamestatuspanel = new GameStatusPanel(matrixKleur);
				
					spelpaneel.add(gamestatuspanel, BorderLayout.CENTER);
				
				hoofdpaneel1_1.add(serverpaneel);
				hoofdpaneel1_1.add(jurymasterpaneel);
				hoofdpaneel1_1.add(spelerspaneel);
				hoofdpaneel1_1.add(spelpaneel);
		
			hoofdpaneel1_2 = new JPanel(new BorderLayout());
			hoofdpaneel1_2.setOpaque(false);
			hoofdpaneel1_2.setBorder(serverLogBorder);
			
				GameLogPanel gamelogpanel = new GameLogPanel();
			
			hoofdpaneel1_2.add(gamelogpanel,    BorderLayout.CENTER);
				
		hoofdpaneel.add(hoofdpaneel1_1);
		hoofdpaneel.add(hoofdpaneel1_2);
			
		add(hoofdpaneel, BorderLayout.CENTER);
		
		//Verzameling van al de statuspanels 
		updatepanels = new UpdatePanels(serverstatuspanel, jmstatuspanel, playerstatuspanel, gamestatuspanel, gamelogpanel );


		startServer();
	}
		
	/// <summary>
	/// Deze method instantieert de nodige borders van de server GUI
	/// </summary>
	/// <author>Sebastian Kubski</author>
	private void createBorders() {	
		serverStatusBorder  = BorderFactory.createTitledBorder(labelContents[0]);
		serverStatusBorder.setTitleJustification(TitledBorder.CENTER);
		serverStatusBorder.setTitleFont(new Font("Arial", Font.BOLD, 18));
		connectionBorder  = BorderFactory.createTitledBorder(labelContents[1]);
		connectionBorder.setTitleJustification(TitledBorder.LEFT);
		juryBorder  = BorderFactory.createTitledBorder(labelContents[2]);
		juryBorder.setTitleJustification(TitledBorder.LEFT);
		playerBorder  = BorderFactory.createTitledBorder(labelContents[3]);
		playerBorder.setTitleJustification(TitledBorder.LEFT);
		gameStatusBorder= BorderFactory.createTitledBorder(labelContents[4]);
		gameStatusBorder.setTitleJustification(TitledBorder.LEFT);
		serverLogBorder = BorderFactory.createTitledBorder(labelContents[5]);
		serverLogBorder.setTitleJustification(TitledBorder.LEFT);
	} 
		
	/// <summary>
	/// Deze method boot de serverside
	/// </summary>
	/// <author>Sebastian Kubski</author>
	private void startServer() {		
		//Settings en updatepanels objecten meegeven aan de servers!
		//Server @ PORT 5000
		Server server = new Server(updatepanels);
		//ExecutorService
		ExecutorService threadExecutor = Executors.newCachedThreadPool();
		threadExecutor.execute( server );
	}
	
}
